package com.mingyizhudao.app.view.mine;

import java.util.List;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import com.dixiang.framework.common.QjResult;
import com.dixiang.framework.widget.LoadingDialog;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.common.IDataLoadListenter;
import com.mingyizhudao.app.http.DataUtils;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.utils.IntentHelper;
import com.mingyizhudao.app.view.MYBaseActivity;
import com.mingyizhudao.app.view.mine.adpter.OrderItemAdpter;
import com.mingyizhudao.app.view.mine.bean.OrderItemEntity;

public class OrderListActivity extends MYBaseActivity{
	private ListView listView;
	private OrderItemAdpter adpter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_list);
		initView();
		doNet();
	}
	
	private void initView(){
		activity = this;
		listView = (ListView)findViewById(R.id.lv_order);
		adpter = new OrderItemAdpter(this );
		listView.setOnItemClickListener(onItemClickListener);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	public void initHeaderView() {
		setTopViewBg(R.color.actionbar_bg_color);
		initBackView(R.drawable.left_back_white_icon, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		setTitleViewValue(R.string.order_list_title, 0, R.color.white);
	}
	
	public void doNet(){
		LoadingDialog.getInstance(activity).show();
    	DataUtils.getInitial().getBookingList(activity, null, loadListenter);
    }
    
    private OnItemClickListener onItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {	
//			OrderItemEntity item = adpter.getItem(arg2);
//			IntentHelper.getInstance(activity).gotoActivityWithURL(OrderDetailActivity.class, item.getActionUrl());
			
			OrderItemEntity item = adpter.getItem(arg2);
			Bundle bundle = new Bundle();
			bundle.putSerializable( Constants.PARM_ACTION_URL, item.getActionUrl());
			bundle.putSerializable("orderItemEntity", item);
			IntentHelper.getInstance(activity).gotoActivity(OrderStateCommonActivity.class, bundle);
		}
	};
	
	private IDataLoadListenter loadListenter = new IDataLoadListenter() {
		
		@Override
		public void onSuccess(final Object result) {
			handler.post(new Runnable() {
				
				@Override
				public void run() {
					if(result != null){
						List<OrderItemEntity> list = (List<OrderItemEntity>)result;
						if(list.size() > 0){
							adpter.clear();
							adpter.addAll(list);
		        			listView.setAdapter(adpter);
		        			return;
						}
					}
					Toast.makeText( activity, "暂时没有数据！", 0 ).show();					
				}
			});
		}
		
		@Override
		public void onFailure(Object result) {
			QjResult<List<OrderItemEntity>> value = (QjResult<List<OrderItemEntity>>) result;
			if(value != null) CommonUtils.showErrorAtBottom(activity, handler, value.getErrorMsg());
		};
		
		@Override
		public void onError(Exception e, Object result) {
			if(e != null) CommonUtils.showErrorAtBottom(activity, handler, e.getMessage());
		}
		
		@Override
		public void onHttpError(int msg) {
			CommonUtils.showHttpError(activity, handler, msg);
		};
		
		@Override
		public void onCompleted(Exception e, Object result) {
			LoadingDialog.getInstance(activity).dismiss();
		}
	};

}
